﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace LateNight.Behaviors
{
    public class IncrementalAnimationTrigger : TriggerAction<FrameworkElement>
    {
        public Storyboard Storyboard
        {
            get { return (Storyboard)GetValue(StoryboardProperty); }
            set { SetValue(StoryboardProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Storyboard.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StoryboardProperty =
            DependencyProperty.Register("Storyboard", typeof(Storyboard),
            typeof(IncrementalAnimationTrigger), null);


        public static readonly DependencyProperty TimeOffSetProperty = DependencyProperty.RegisterAttached("TimeOffSet",
                                        typeof(double),
                                        typeof(IncrementalAnimationTrigger),
                                        null);
        public double TimeOffSet
        {
            get { return (double)GetValue(TimeOffSetProperty); }
            set { SetValue(TimeOffSetProperty, value); }
        }

        //guardamos el último que se creó para usarlo como 
        //referencia para el retardo
        static Storyboard lastStoryboard;

        protected override void OnAttached()
        {
            base.OnAttached();
        }

        protected override void Invoke(object parameter)
        {
            if (Storyboard != null)
            {
                Storyboard.Stop();

                //aquí miramos si hay un storyboard ya
                if (lastStoryboard != null)
                {
                    //si el storyboard aún no ha empezado o hace poco que lo ha hecho...
                    if (lastStoryboard.GetCurrentTime() < TimeSpan.FromMilliseconds(TimeOffSet))
                    {
                        //calculamos un retraso en milisegundos para que empiece el 
                        //siguiente
                        TimeSpan begin = TimeSpan.FromMilliseconds(TimeOffSet) -
                            lastStoryboard.GetCurrentTime();
                        if (lastStoryboard.BeginTime != null)
                        {
                            begin += lastStoryboard.BeginTime.Value;
                        }
                        Storyboard.BeginTime = begin;
                    }
                }
                //guardamos el de ahora como el último
                lastStoryboard = Storyboard;
                // no consigo hacer esto en el xaml
                Storyboard.SetTarget(this.Storyboard.Children[0], this.AssociatedObject);
                Storyboard.Begin();
            }
        }
    }
}